package com.wolfking.demo.nio.file;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;

public class FileCopyByteBuffer {
    public static void main(String[] args) throws Exception {

        FileChannel readChannel = FileChannel.open(new File("C:\\ssl_key\\ssl.log").toPath(), StandardOpenOption.READ);
        File target = new File("H:\\ssl.txt");
        if (!target.exists()) {
            if (!target.getParentFile().exists()) {
                target.getParentFile().mkdirs();
            }
            target.createNewFile();
        }
        FileChannel writeChannel = FileChannel.open(target.toPath(), StandardOpenOption.WRITE);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (readChannel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            writeChannel.write(byteBuffer);
            byteBuffer.clear();
        }
        writeChannel.force(true);
        readChannel.close();
        writeChannel.close();
    }
}
